package com.um.jdy.platform.recharge.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.member.entity.dto.RechargePackageDTO;
import com.um.jdy.common.member.entity.po.RechargePackage;
import com.um.jdy.common.member.entity.vo.RechargePackageVO;
import com.um.jdy.common.member.service.RechargePackageService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 邓南辉
 * 日期 2023/6/25
 * 版权所有 广州优匠科技有限公司
 */

@Component
public class RechargePackageManager extends BaseManager<RechargePackageService> {

    /**
     * 分页查询站场
     * @param params
     * @return
     */
    public Response<Page<RechargePackageVO>> page(PageParams params){
        Page page = new Page();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        page = service.getPage(page,params.getProperties(),params.getValues(),params.getOrderNames(),params.getSortOns());
        return Response.success(page);
    }

    public Response<RechargePackage> addEntity(RechargePackageDTO dto) {
        RechargePackage entity = BeanUtil.toBean(dto, RechargePackage.class);
        service.add(entity);
        return Response.success(entity);
    }

    public Response<RechargePackage> updateEntity(String id, RechargePackageDTO dto) {

        if (null == service.getEntity( id)) {
            throw new ServiceException("套餐不存在！");
        }

        RechargePackage entity = BeanUtil.toBean(dto, RechargePackage.class);
        entity.setMemberRechargePackageId(id);
        service.updateEntity(entity);
        return Response.success(entity);
    }


    public Response delete(String id) {
        boolean result = this.service.delete(id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        boolean result = service.deleteIds(ids);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response enable(String id, String enable) {
        RechargePackage entity = this.service.getEntity(id);
        entity.setIsEnable(enable);
        boolean result = this.service.update(entity);
        return result ? Response.success() : Response.failed();
    }
}
